﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Krilib.Control.Surface.MessageObjects
{
    public class Hyperlink : Text,IDisposable
    {
        public Uri Target { get; set; }

        Font internalFont = new Font(Bridge.Config.Font, FontStyle.Underline);
        protected override Font InternalFont
        {
            get { return internalFont; }
        }

        public Hyperlink(Uri uri)
            : this(uri, uri.OriginalString, Bridge.Config.LinkColor) { }

        public Hyperlink(Uri uri, string linkText)
            : this(uri, linkText, Bridge.Config.LinkColor) { }

        public Hyperlink(Uri uri, String linkText, Color forecolor)
            : base(linkText, forecolor)
        {
            Target = uri;
        }

        ~Hyperlink()
        {
            if (!disposed)
                this.Dispose();
        }

        public override string HelptipText
        {
            get
            {
                if (Target.OriginalString.Length > 30)
                    return ">" + Target.OriginalString.Substring(0, 30) + "...";
                else
                    return ">" + Target.OriginalString;
            }
        }

        public override void OnMouseEnter(object sender, ChainMouseEventArgs e)
        {
            this.ForeColor = Color.FromArgb(
                Convert.ToInt32((byte)(~Bridge.Config.LinkColor.R)),
                Convert.ToInt32((byte)(~Bridge.Config.LinkColor.G)),
                Convert.ToInt32((byte)(~Bridge.Config.LinkColor.B)));
            Bridge.CommonOperation.SetCursor(Cursors.Hand);
        }

        public override void OnMouseLeave(object sender, ChainMouseEventArgs e)
        {
            this.ForeColor = Bridge.Config.LinkColor;
            Bridge.CommonOperation.SetCursor(Cursors.Default);
        }

        public override void OnMouseClick(object sender, ChainMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                Bridge.CommonOperation.CallBrowser(Target);
            else
                e.SetThrowSub();
        }

        bool disposed = false;
        public void Dispose()
        {
            internalFont.Dispose();
            internalFont = null;
            disposed = true;
        }
    }
}
